//
//  TPUserRootViewController.m
//  Yimei
//
//  Created by xiaowu xiaowu on 2017/10/20.
//  Copyright © 2017年 xiaowu xiaowu. All rights reserved.
//

#import "TPUserRootViewController.h"
#import "YPUserRootHeaderView.h"
#import "TPUserInfoViewController.h"
#import "TPUserBookingViewController.h"
#import "TPUserSetViewController.h"
#import "TPUserOrderViewController.h"
#import "TPAppointmentViewController.h"
@interface TPUserRootViewController()
@property (nonatomic,copy)NSArray *titleArray;
@property (nonatomic,copy)NSArray *imgArray;
@property (nonatomic,strong)YPUserRootHeaderView *headerView;
@property (nonatomic,copy)NSArray *vcArray;
@end
@implementation TPUserRootViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的";
    self.tableViewStyle = UITableViewStyleGrouped;
    self.tableView.rowHeight =50.0f;
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 50.0, 0);
//    self.tableView.backgroundColor = [UIColor whiteColor];
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0.0f);
    }];
//    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loginSccuess) name:kNotiLoginSccuess object:nil];
    [self setup];
    [self loginSccuess];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.headerView.model = [LoginModel shareLoginModel];
    
    [self.tableView reloadData];
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.translucent = YES;
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent animated:NO];
    
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.isBarWhite = NO;
    self.navigationController.navigationBar.translucent = NO;
    
}
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    [[LoginModel shareLoginModel]getInfoModel];
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            if([LoginModel shareLoginModel].user_name.length == 0){
                 [UIAlertView alertWithCallBackBlock:^(NSInteger buttonIndex) {
                     if(buttonIndex == 0)
                         return ;
                      [self.navigationController pushViewController:[TPUserInfoViewController new] animated:YES];
                 } title:@"温馨提示" message:@"您还未完善资料，是否去完善" cancelButtonName:@"取消" otherButtonTitles:@"去完善", nil];
            }
        });
    
}
-(void)rightBtnAction:(id)sender{
    
}
-(void)loginSccuess{
    
        self.titleArray = @[@[@"我的收藏",@"联系我们"],@[@"意见反馈",@"关于美臣"],@[@"更多设置"]];
        self.imgArray = @[@[@"TP_user_collection",@"TP_user_contact"],@[@"TP_user_feedboock",@"TP_user_about"],@[@"TP_user_set"]];
        self.vcArray = @[@[@"TPUserCollectionViewController",@""],@[@"TPFeedbackViewController",@"TPUserAboutViewController"],@[@"TPUserSetViewController"]];
    
        [self.tableView reloadData];
    
}


-(void)setup{
    
    
    
    //    self.titleArray = @[@"我的收藏",@"企业入驻",@"分享",@"意见反馈",@"更多设置"];
    //    self.vcArray = @[@"YPMyCollectionViewController",@"YPInEnterpriseViewController",@"TPSigninViewController",@"TPFeedbackViewController",@"YPMoreSetViewController"];
    //
    self.headerView = [[YPUserRootHeaderView alloc]initWithFrame:CGRectMake(0, 0, IPHONE_WIDTH, 300.0)];
    self.tableView.tableHeaderView = self.headerView;
    @weakify(self);
    [_headerView setActionButtonBlock:^(NSInteger tag,UIButton *sender){
        
        @strongify(self);
        
        if(!xw_getLogin())
            return;
        
        switch (tag) {
           
            case YPUserRootHeaderViewButtonStyleEdior:
            case YPUserRootHeaderViewButtonStyleActor:
            {
                [self.navigationController pushViewController:[TPUserInfoViewController new] animated:YES];
            }
                break;
            case YPUserRootHeaderViewButtonStyleNursing:{ //护理记录
                TPAppointmentViewController *bookingVC = [TPAppointmentViewController new];
                bookingVC.type = TPAppointmentTypeAppoint;
                bookingVC.title = sender.titleLabel.text;
                [self.navigationController pushViewController:bookingVC animated:YES];
            }
                break;
            case YPUserRootHeaderViewButtonStyleBooking:{ //预约
                TPAppointmentViewController *bookingVC = [TPAppointmentViewController new];
                bookingVC.type = TPAppointmentTypeBooking;
                bookingVC.title = sender.titleLabel.text;
                [self.navigationController pushViewController:bookingVC animated:YES];
            }
                break;
            case YPUserRootHeaderViewButtonStyleOrder:{
                TPUserOrderViewController *userOrder = [TPUserOrderViewController new];
                [self.navigationController pushViewController:userOrder animated:YES];
            }
                break;
          
                
            default:
                break;
        }
    }];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.titleArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.titleArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifir = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifir];
    
    if(!cell){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifir];
        
        cell.textLabel.font = fontSystemOfSize(16);
        cell.detailTextLabel.font = fontSystemOfSize(15);
        cell.detailTextLabel.textColor = TPBlack_999999;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    
    cell.imageView.image = ImageNamed(self.imgArray[indexPath.section][indexPath.row]);
    cell.detailTextLabel.text = @"";
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.text = [NSString stringWithFormat:@" %@",self.titleArray[indexPath.section][indexPath.row]];
    
    if(indexPath.section == 0 && indexPath.row == 1){
        cell.detailTextLabel.text = @"02080922663";
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1.0f;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   
    if(!xw_getLogin())
        return;

    switch (indexPath.section) {
            
        case 0:{
       
              if(indexPath.row == 1)
                [XWCommon getPhoneNuber:@"02080922663" view:self.view];
            
            
        }
            break;
       
            
        default:
            break;
    }
    [self.navigationController pushViewController:[NSClassFromString(self.vcArray[indexPath.section][indexPath.row]) new] animated:YES];
    
}
@end
